from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


# 自定义等待条件函数
def element_has_text(locator, text):
    def check(driver):
        element = driver.find_element(*locator)
        return text in element.text

    return check


# 初始化浏览器驱动
driver = webdriver.Chrome()

# 访问一个网址
driver.get("https://www.jd.com")

try:
    # 使用显式等待结合自定义条件
    element = WebDriverWait(driver, 10).until(
        element_has_text((By.CLASS_NAME, "logo"), "京东")
    )
    print("目标元素满足条件")

    # 截图保存
    driver.save_screenshot("wait_page05.png")
    print("截图已保存")

except Exception as e:
    print(f"等待条件未满足：{e}")

finally:
    # 关闭浏览器
    driver.quit()